﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows;

namespace CarAppClient.Controls.NoticeBarUserControls
{
    /// <summary>
    /// 公告栏
    /// </summary>

    [TemplatePart(Name = nameof(PART_ContentPresenter), Type = typeof(ContentPresenter))]
    [TemplatePart(Name = nameof(PART_Grid), Type = typeof(Grid))]
    public class ExtNoticeBar : ContentControl
    {
        #region 基础属性
        /// <summary>
        /// 圆角
        /// </summary>
        [Bindable(true)]
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ExtNoticeBar));

        /// <summary>
        /// 控件边框阴影
        /// </summary>
        [Bindable(true)]
        public Color ShadowColor
        {
            get { return (Color)GetValue(ShadowColorProperty); }
            set { SetValue(ShadowColorProperty, value); }
        }
        public static readonly DependencyProperty ShadowColorProperty = DependencyProperty.Register("ShadowColor", typeof(Color), typeof(ExtNoticeBar));

        /// <summary>
        /// 鼠标移入边框色
        /// </summary>
        [Bindable(true)]
        public Brush HoverBorderBrush
        {
            get { return (Brush)GetValue(HoverBorderBrushProperty); }
            set { SetValue(HoverBorderBrushProperty, value); }
        }
        public static readonly DependencyProperty HoverBorderBrushProperty = DependencyProperty.Register("HoverBorderBrush", typeof(Brush), typeof(ExtNoticeBar), new PropertyMetadata(Brushes.Transparent));

        /// <summary>
        /// 鼠标移入背景颜色
        /// </summary>
        [Bindable(true)]
        public Brush HoverBackground
        {
            get { return (Brush)GetValue(HoverBackgroundProperty); }
            set { SetValue(HoverBackgroundProperty, value); }
        }
        public static readonly DependencyProperty HoverBackgroundProperty = DependencyProperty.Register("HoverBackground", typeof(Brush), typeof(ExtNoticeBar), new PropertyMetadata(Brushes.Transparent));

        #endregion

        private DoubleAnimation? animation;
        private Storyboard? storyboard;
        private Grid? PART_Grid;
        private ContentPresenter? PART_ContentPresenter;
        public object InnerLeftContent
        {
            get { return (object)GetValue(InnerLeftContentProperty); }
            set { SetValue(InnerLeftContentProperty, value); }
        }
        public static readonly DependencyProperty InnerLeftContentProperty = DependencyProperty.Register("InnerLeftContent", typeof(object), typeof(ExtNoticeBar));

        public double Duration
        {
            get { return (double)GetValue(DurationProperty); }
            set { SetValue(DurationProperty, value); }
        }
        public static readonly DependencyProperty DurationProperty = DependencyProperty.Register("Duration", typeof(double), typeof(ExtNoticeBar), new PropertyMetadata(20.0));

        public object InnerRightContent
        {
            get { return (object)GetValue(InnerRightContentProperty); }
            set { SetValue(InnerRightContentProperty, value); }
        }
        public static readonly DependencyProperty InnerRightContentProperty = DependencyProperty.Register("InnerRightContent", typeof(object), typeof(ExtNoticeBar));

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            PART_ContentPresenter = GetTemplateChild(nameof(PART_ContentPresenter)) as ContentPresenter;
            PART_Grid = GetTemplateChild(nameof(PART_Grid)) as Grid;
        }

        void InitAnimation()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
            {
                if (Content == null) return;
                if (storyboard == null) storyboard = new Storyboard() { RepeatBehavior = RepeatBehavior.Forever };
                storyboard.Stop();
                animation = new DoubleAnimation()
                {
                    From = PART_Grid?.ActualWidth,
                    To = -PART_ContentPresenter?.ActualWidth,
                    Duration = TimeSpan.FromSeconds(Duration),
                };
                storyboard.Children.Add(animation);
                Storyboard.SetTargetProperty(animation, new PropertyPath("(ContentPresenter.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));
                Storyboard.SetTarget(animation, PART_ContentPresenter);
                storyboard.Begin();
            }));
        }
        protected override Size MeasureOverride(Size constraint)
        {
            InitAnimation();
            return base.MeasureOverride(constraint);
        }
    }
}
